Added defensive "before" validator in DagRunContext to handle DetachedInstanceError#59857
Merged
Lee-W merged 1 commit intoapache:mainfrom Jan 22, 2026
Merged
Conversation
8dcf9a4 to
095248d
Compare
Lee-W
reviewed
Jan 5, 2026
be3315c to
fac79a3
Compare
efc8cd9 to
796b081
Compare
Lee-W
approved these changes
Jan 9, 2026
Contributor
Author
Member
|
Nothing is needed on your end. I just want to wait for a second pair of eyes :) let's wait for a bit longer. |
suii2210
pushed a commit
to suii2210/airflow
that referenced
this pull request
Jan 26, 2026
…ts included. (apache#59857) Co-authored-by: Sameer Mesiah <smesiah971@gmail.com>
shreyas-dev
pushed a commit
to shreyas-dev/airflow
that referenced
this pull request
Jan 29, 2026
…ts included. (apache#59857) Co-authored-by: Sameer Mesiah <smesiah971@gmail.com>
jhgoebbert
pushed a commit
to jhgoebbert/airflow_Owen-CH-Leung
that referenced
this pull request
Feb 8, 2026
…ts included. (apache#59857) Co-authored-by: Sameer Mesiah <smesiah971@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A defensive (
mode = "before") Pydantic validator has been added toDagRunContextto safely handle detached ORMDagRunobjects. The validator reloads theDagRunobject from the DB withconsumed_asset_eventswhen relationship access would otherwise raiseDetachedInstanceError, preventing scheduler crashes inDagRuntimeout and callback paths. This complements existing eager-loading fixes added in PR #56916 and PR #59714.Rationale
DagRunContextmay be constructed with ORMDagRuninstances that are no longer session-bound (e.g. during scheduler timeout handling). Accessing lazily loaded relationships such asconsumed_asset_eventson detached instances can raiseDetachedInstanceError, which currently propagates and causes the scheduler to exit unexpectedly.This issue has surfaced multiple times in different
DagRuncode paths. Centralizing a defensive guard inDagRunContextensures that missing or detached relationship data does not crash the scheduler. Using a session to reload the DagRun within the validator is an acceptable tradeoff to maintain correctness with regards to the state ofconsumed_asset_events. Also, it is assumed that the reloadedDagRunentity must exist in the DB for code paths whereDagRunContextis called.Note that this validator only applies to ORM
DagRunobjects and not fastapi data models.Tests
DagRunContextconstruction with a detached ORMDagRun, asserting thatconsumed_asset_eventsis present afterwards.DagRunpath to ensure existing behavior is preserved.Closes: #59740